package com.laicunba.event.listener;

import java.math.BigDecimal;
import java.util.Date;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;

import com.laicunba.event.PaySuccessEvent;
import com.laicunba.pojo.ActivityDO;
import com.laicunba.pojo.FinancingTargetBidDO;
import com.laicunba.service.ActivityService;
import com.laicunba.service.FinancingTargetService;
import com.laicunba.service.RateIncrCouponDefService;
import com.laicunba.service.RegistryService;
import com.laicunba.service.UserService;
import com.laicunba.service.VcoinWalletService;
import com.laicunba.util.DateUtil;

//@Component
public class NewYearListener implements ApplicationListener<PaySuccessEvent> {
	private static final Logger LOG = Logger.getLogger(NewYearListener.class);

	@Autowired
	private RegistryService registryService;
	@Autowired
	private RateIncrCouponDefService rateIncrCouponDefService;
	@Autowired
	private FinancingTargetService financingTargetService;
	@Autowired
	private UserService userService;
	@Autowired
	private VcoinWalletService vcoinWalletService;
	@Autowired
	private ActivityService activityService;

	@Override
	public void onApplicationEvent(PaySuccessEvent event) {
		try {
			FinancingTargetBidDO bidDO = (FinancingTargetBidDO) event.getSource();
			ActivityDO activity = activityService.get(ActivityDO.class, "20161224");
			if (activity == null) {
				return;
			}
			if (!"CPB".equalsIgnoreCase(bidDO.getType())) {
				return;
			}
			if (activity.getBeginTime() == null || activity.getEndTime() == null) {
				return;
			}

			long payTime = bidDO.getPayTime().getTime();
			if (payTime < activity.getBeginTime().getTime() || payTime > activity.getEndTime().getTime()) {
				return;
			}

			if (bidDO.getEndProfit() == null) {
				return;
			}

			int days = DateUtil.days(new Date(), bidDO.getEndProfit());

			if (days >= 30 && days < 70) {

				vcoinWalletService.add(bidDO.getBidUserId(),
						bidDO.getBuyAmount().divideToIntegralValue(new BigDecimal(1000)).longValue() * 1,
						"NEW_YEAR_ACT", "运营-双旦活动");

			} else if (days >= 70 && days < 80) {
				vcoinWalletService.add(bidDO.getBidUserId(),
						bidDO.getBuyAmount().divideToIntegralValue(new BigDecimal(1000)).longValue() * 3,
						"NEW_YEAR_ACT", "运营-双旦活动");
			} else if (days >= 80 && days < 120) {
				vcoinWalletService.add(bidDO.getBidUserId(),
						bidDO.getBuyAmount().divideToIntegralValue(new BigDecimal(1000)).longValue() * 4,
						"NEW_YEAR_ACT", "运营-双旦活动");
			} else if (days >= 120 && days < 150) {

				vcoinWalletService.add(bidDO.getBidUserId(),
						bidDO.getBuyAmount().divideToIntegralValue(new BigDecimal(1000)).longValue() * 9,
						"NEW_YEAR_ACT", "运营-双旦活动");
			} else if (days >= 150) {
				vcoinWalletService.add(bidDO.getBidUserId(),
						bidDO.getBuyAmount().divideToIntegralValue(new BigDecimal(1000)).longValue() * 11,
						"NEW_YEAR_ACT", "运营-双旦活动");
			}

		} catch (Exception e) {
			LOG.error("【双旦活动】积分发放出错了", e);
		}
	}

}
